package com.hualan.sfzy.controller.counselor;


import com.hualan.sfzy.entity.Question;
import com.hualan.sfzy.entity.SysUser;
import com.hualan.sfzy.service.psyc.QuestionService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/question")
    public Result add(@RequestBody Question question){
        return questionService.add(question)>0?Result.success():Result.error();
    }
    @DeleteMapping("/question/{id}")
    public Result removeById(@PathVariable Integer id){
        return questionService.removeById(id)>0?Result.success():Result.error();
    }
    @DeleteMapping("/question")
    public Result remove(Integer[] ids){
        return questionService.remove(ids)>0?Result.success():Result.error();
    }
    @PutMapping("/question")
    public Result edit(@RequestBody Question question){
        return questionService.edit(question)>0?Result.success():Result.error();
    }
    @GetMapping("/question/{id}")
    public Result<Question> findById(@PathVariable Integer id){
        Question question = questionService.findById(id);
        return Result.success(question);
    }
    @GetMapping("/question")
    public Result<List<Question>> find(){
        List<Question> questions=questionService.find();

        return Result.success(questions);
    }
//    @GetMapping("/question/pages")
//    public Result<Map<String, Object>> findPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
//                                    @RequestParam(value = "pageSize",required = false,defaultValue = "10")  Integer pageSize,
//                                    Question question){
//        Map<String, Object> map=questionService.findPage(pageNum,pageSize,question);
//        return Result.success(map);
//
//
//    }
@GetMapping("/question/pages")
public Result<Map<String, Object>> findPage(
        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
        @RequestParam(value = "questionTitle", required = false) String questionTitle,
        @RequestParam(value = "username", required = false) String username
) {
    Question question = new Question();
    question.setTitle(questionTitle);
    if (username != null && !username.isEmpty()) {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        question.setSysUser(sysUser);
    }
    Map<String, Object> map = questionService.findPage(pageNum, pageSize, question);
    return Result.success(map);
}

}
